package
{
	import controllers.VideoController;
	
	import flash.external.ExternalInterface;
	
	import org.dexter.sendDexterEvent;
	
	import vo.GUserVO;

	public class VideoCtrl2 extends VideoController
	{
		[DexterBinding]
		public var userListCtrl:UserListCtrl2;
		public var useFMLE:Boolean;
		public function VideoCtrl2()
		{
		}
		[DexterEvent]
		public function initPublishVideo():void{
			var publishers:Object = userListCtrl.list.getItemsMapByType("publishPos");
			for(var i:String in publishers){
				var index:int = Number(i);
				if(index == 0)continue;
				for each(var user:GUserVO in publishers[i]){
					publisherChange(user,index);
				}
			}
		}
		[DexterEvent]
		public function publisherChange(user:GUserVO,value:int):void{
			sendDexterEvent("callJs","jsTrace","useFMLE:"+useFMLE);
			if(value == 0){
				if(user.isSelf && !useFMLE){
					if(outStream)outStream.publish();
				}else{
					if(getStream(user.id))getStream(user.id).play(false);
				}
				useFMLE = false;
				sendDexterEvent("setVideo",null,user.publishPos);
			}else{
				if(user.isSelf){
					if(useFMLE){
						playStream(user.id);
						//getStream(user.id).receiveAudio(false);
					}
					else publishStream();
				}else{
					playStream(user.id);
				}
				sendDexterEvent("setVideo",user.id,value);
			}
		}
		[DexterEvent]
		public function audioChange(user:GUserVO,value:int):void{
			if(value == 0){
				if(user.isSelf)
					if(outStream)outStream.publish();
				else
					if(getStream(user.id))getStream(user.id).play(false);
			}else if(value == 2){
				if(user.isSelf){
					publishStream(false);
				}else{
					playStream(user.id);
				}
			}
		}
		[DexterExternal]
		public function publishVideo(id:String,index:int = 1):void{
			if(index > 0){
				stopPublish(index);
			}
			sendDexterEvent("changeUserPropertyFromJs",id,"publishPos",index);
		}
		[DexterExternal]
		public function setBufferTime(bf:Number):void{
			changeBufferTime(Number(bf));
			localSetting.bufferTime = Number(bf);
		}
		[DexterEvent]
		public function stopPublish(index:int = 1):void{
			var user:GUserVO = userListCtrl.list.getItemsByType("publishPos",index).getItemAt(0) as GUserVO;
			if(user){
				sendDexterEvent("changeUserPropertyFromJs",user.id,"publishPos",0);
			}
		}
	}
}